﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using System.IO;

using ICSharpCode.SharpZipLib.Zip;

using ScrumTable.Common.FileSystem;

#endregion

namespace ScrumTable.Common.Zip
{
    /// <summary>
    /// Contains the functionalities to add a file to a zip enclosure. Before
    /// adding the file to the zip, it will be copied to a temporary directory
    /// in order to avoid file access problems.
    /// </summary>
    public class FileInfoZipper : DisposeBase, IStaticDataSource
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        private readonly FileInfo _fileToZip;
        private readonly IDisposable _fileToZipReleaseHandle;

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        /// <summary>
        /// Gets the name of the contained file.
        /// </summary>
        public string Name
        {
            get { return _fileToZip.Name; }
        }

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        /// <summary>
        /// Creates a new FileInfoZipper instance.
        /// </summary>
        /// <param name="filePath">Specifies the path of the file to add to the zip.</param>
        public FileInfoZipper(string filePath)
        {
            PreCondition.AssertNotNullOrEmpty(filePath, "filePath");

            _fileToZipReleaseHandle = new FileInfo(filePath).TemporaryCopy(out _fileToZip);
        }

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Gets the source which is used by the #ZipLib.
        /// </summary>
        Stream IStaticDataSource.GetSource()
        {
            return _fileToZip.OpenRead();
        }

        /// <summary>
        /// Releases managed (c# or .net) resources.
        /// </summary>
        protected override void ReleaseManagedResources()
        {
            base.ReleaseManagedResources();

            _fileToZipReleaseHandle.Dispose();
        }

        #endregion
    }
}
